package com.itheima.protocol.clone;

import java.io.*;

public class Student implements Serializable {

    private static final long serialVersionUID = 2194647596561762055L;

    private String name;
    private int age;

    private Person person;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public Object deepClone() throws Exception{

        //序列化
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);

        //反序列化
        ByteArrayInputStream bais = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream inputStream = new ObjectInputStream(bais);

        return inputStream.readObject();
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", person=" + person +
                '}';
    }
}
